package br.com.foxinline.servlet;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author laverson
 */
@WebServlet(name = "PrevisualizarImagemServlet", urlPatterns = "/previsualizar/*")
public class PrevisualizarImagemServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String filename = request.getPathInfo().substring(1);

        String optPath = "tmp/Atlas/uploads/";

        File file = new File("/" + optPath, filename);

        getFileCopy(response, filename, file);
    }

    private void getFileCopy(HttpServletResponse response, String filename, File file) throws IOException {
        response.setHeader("Content-Type", getServletContext().getMimeType(filename));
        response.setHeader("Content-Length", String.valueOf(file.length()));
        response.setHeader("Content-Disposition", "inline; filename=\"" + filename + "\"");
        Files.copy(file.toPath(), response.getOutputStream());
    }
}
